Add Bunch methods to allow live updates and loading from json #170
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a request for feedback to address #12 and #14 as it technically has some breaking api changes and I'm unsure what kinds of guarantees you have with downstream users.
New Features
Bunch(**correctly_formatted_provider_dict)
now makes a providers object from whatever's in the dict.providers[new_provider_name] = new_provider
now works whenevernew_provider_name
is a str andnew_provider
is aBunch
, aTileProvider
or a correctly formatted dict representing a Bunch or TileProviderproviders.update(**{provider_name: new_provider, ...})
Now works whereverprovider_name
andnew_provider
match the__setitem__
criteria._load_json
function. We could reasonably make this also load json files or ditch the method entirely and haveproviders.py
make the json.loads call.Breaking changes
__init__
,__setitem__
andupdate
no longer function like theirdict
counterparts for theBunch
class. I'd be suprised if anyone depended on this functionality though.isinstance(TileProvider, Bunch)
is no longer true, andTileProvider
no longer has access toBunch
methods, consequently. Instead,TileProvider
directly inherits fromdict
and implements it's own copy of__getattr__
. I think this is technically more in line with what you want, but may have consequences I can't see.Other thoughts
If you can provide more guidance about downstream use cases, I think it would be worth making the following adjustments:
Bunch
fromdict
tocollections.UserDict[str, Bunch | TileProvider]
and adding in appropriate runtime validation to ensure it stays consistentTileProvider
to either apydantic.BaseModel
or aUserDict[str, str | int]
and providing validation for required and optional fields.I think both changes would provide better usability (ie would fail fast and noisily) if downstream users are adding their own sources and formatting them poorly.